$import("com.joypard.platform.security.role.RoleGridCompoent");
$import("com.joypard.platform.security.role.roleusers.RoleUsersDI");
$import("com.joypard.platform.security.role.roleusers.AssignUserWinView");

/**
 * 角色分配用户管理-视图层
 */
K.defView("com.joypard.platform.security.role.roleusers.RoleUsersView", {

	purviewConfig : {
		assingUser : "platform.security.role.roleUser.assingUser",
		evictUser : "platform.security.role.roleUser.evictUser"
	},

	// 初始化
	initialize : function() {
		this.assignUserWinView = AssignUserWinView.instance({
			roleUsersView : this
		});
		this.initEvents();
		this.initListeners();
	},

	// 初始化事件
	initEvents : function() {
		E.addEvents("evictRolesEvent");
	},

	// 视图层监听事件
	initListeners : function() {
		E.addListener("evictRolesSuccessEvent", this.onEvictRolesSuccess, this);
	},

	// 渲染后初始化视图
	render : function() {

		this.westRolePanel = this.createWestRolePanel(); // 左边的角色列表

		this.searchUserPanel = this.createSearchUsersPanel(); // 中间的用户信息搜索框
		this.userGridPanel = this.createUserGridPanel(); // 中间的用户信息列表

		this.centerPanel = this.createCenterPanel();
		this.centerPanel.add(this.searchUserPanel);
		this.centerPanel.add(this.userGridPanel);

		this.MP = new K.Ext.MainPanel({
			items : [ this.westRolePanel, this.centerPanel ]
		});
	},

	/**
	 * 左边角色搜索面板
	 */
	createWestRolePanel : function() {
		var me = this;
		var westRolePanel = RoleGridCompoent.createCompoentPanel({
			margins : "2 0 2 2",
			split : true
		});
		// 监听点击角色列表事件
		RoleGridCompoent.roleGridPanel.getSelectionModel().on("rowselect", function(sm, rowIndex, r) {
			me.assignUserWinView.roleId = r.id;
			$("#the-user-search input[name='roleIds']").val(r.id);
			me.userGridPanel.getStore().load({
				params : K.serializeJson("#the-user-search")
			});
		});
		// 监听点击角色列表加载数据事件
		RoleGridCompoent.roleGridPanel.getStore().on("load", function() {
			if (this.getCount() > 0) {
				RoleGridCompoent.roleGridPanel.getSelectionModel().selectRow(0);
			} else {
				me.userGridPanel.getStore().removeAll();
			}
		});
		return westRolePanel;
	},

	/**
	 * 中间用户面板
	 */
	createCenterPanel : function() {
		var me = this;
		var options = {
			region : "center",
			border : false,
			layout : 'border',
			margins : '2 2 2 0',
			split : true
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 创建搜索用户表单
	 */
	createSearchUsersPanel : function() {
		var options = {
			region : 'north',
			collapsible : true,
			frame : true,
			margins : "0 0 2 0",
			contentEl : "the-user-search",
			bodyCssCls : "search-gb",
			iconCls : "p-search",
			title : "用户查询",
			border : true
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 创建用户列表面板
	 */
	createUserGridPanel : function() {
		var sm = new Ext.grid.CheckboxSelectionModel({});
		var options = {
			title : "用户列表",
			region : "center",
			iconCls : "p-tab",
			viewConfig : {
				forceFit : true
			},
			border : true,
			store : RoleUsersDI.createUserStore(),
			loadMask : true,
			sm : sm,
			columns : [ sm, {
				header : "用户编码",
				width : 1.5,
				sortable : true,
				dataIndex : 'userCode'
			}, {
				id : "userNickName",
				header : "用户名",
				width : 2,
				sortable : true,
				dataIndex : 'userNickName'
			}, {
				header : "账号",
				width : 2,
				sortable : true,
				dataIndex : 'account'
			}, {
				header : "邮箱",
				width : 4,
				sortable : true,
				dataIndex : 'email'
			}, {
				header : "手机",
				width : 3,
				sortable : true,
				dataIndex : 'mobile'
			} ],
			bbar : {
				xtype : "pagination"
			},
			tbar : [ {
				text : "分配",
				hidden : !RoleUsersView.purviewAssingUser(),
				iconCls : "add",
				handler : K.createDelegate(this.onAssingRole, this)
			}, {
				text : "移除",
				hidden : !RoleUsersView.purviewEvictUser(),
				iconCls : 'delete',
				handler : K.createDelegate(this.onEvictRoles, this)
			}, {
				text : "查询",
				iconCls : 'search',
				handler : K.createDelegate(this.onSearchUsers, this)
			} ],
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/** ------ ------ ------ ------ 事件/监听 ------ ------ ------ ------ */

	/**
	 * 分配用户
	 */
	onAssingRole : function() {
		var r = RoleGridCompoent.roleGridPanel.getSelectionModel().getSelected();
		if (r) {
			this.assignUserWinView.createAssignUserPanel(r.id).show();
		} else {
			K.err("请选择角色");
		}
	},

	/**
	 * 移除用户
	 */
	onEvictRoles : function() {
		var r = RoleGridCompoent.roleGridPanel.getSelectionModel().getSelected();
		if (r) {
			var records = this.userGridPanel.getSelectionModel().getSelections();
			if (records && records.length > 0) {
				if (!confirm("您确定要移除吗？")) {
					return;
				}
				var roleId = r.id;
				var usersId = new Array();
				$.each(records, function(i, item) {
					usersId.push(item.id);
				})
				E.fireEvent("evictRolesEvent", {
					roleId : roleId,
					usersId : usersId.join(",")
				});
			} else {
				K.err("请选择用户", this.userGridPanel.tbar);
			}
		} else {
			K.err("请选择左边列表的一个角色");
		}
	},

	/**
	 * 移除用成功
	 */
	onEvictRolesSuccess : function() {
		var records = this.userGridPanel.getSelectionModel().getSelections();
		this.userGridPanel.getStore().remove(records);
		K.msg("移除用户成功", this.userGridPanel.tbar);
	},

	/**
	 * 查询用户
	 */
	onSearchUsers : function() {
		this.userGridPanel.getStore().load({
			params : K.serializeJson("#the-user-search")
		});
	}
})
